package com.yisoo.ubag.product.service.impl;

import com.yisoo.ubag.product.vo.TransferBrandDataVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.Query;

import com.yisoo.ubag.product.dao.BrandDao;
import com.yisoo.ubag.product.entity.BrandEntity;
import com.yisoo.ubag.product.service.BrandService;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        Long cid = Long.valueOf((String)params.get("cid"));
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("brand_id",key).or().like("brand_name",key);

        }

        queryWrapper.eq("parent_id",cid);

        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<TransferBrandDataVo> getEnableBrandByCid(Long cid) {
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",cid);
        List<BrandEntity> brandEntityList = baseMapper.selectList(queryWrapper);
        return brandEntityList.stream().map(brandEntity -> {
            return new TransferBrandDataVo(brandEntity.getBrandId(), brandEntity.getBrandName(), false);
        }).collect(Collectors.toList());
    }

    @Override
    public Integer getNumByParentId(Long userid) {
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",userid);
        return  baseMapper.selectList(queryWrapper).size();
    }

}